package com.fitz.remote.codec;

import com.fitz.remote.model.BasicObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author fitz 2020-5-6 20:13
 */
public class RpcDecoder extends ByteToMessageDecoder {

    private byte serializerType;

    private Class<? extends BasicObject> clazz;

    public RpcDecoder(byte serializerType,Class<? extends BasicObject> clazz) {
        this.serializerType = serializerType;
        this.clazz = clazz;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
        if (byteBuf.readableBytes() < 4) {
            return;
        }
        out.add(Codec.INSTANCE.decode(byteBuf,serializerType,clazz));
    }
}
